package com.menghao.sso.server.model.ticket;

import com.menghao.sso.server.model.validation.Authentication;
import com.menghao.sso.server.registry.ExpirationPolicy;
import com.menghao.sso.server.model.Service;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * 服务票证授予对特定服务的特定访问权。它只适用于特定的服务。一般来说，它是一次性的使用票
 */
public final class ServiceTicketImpl extends AbstractTicket implements
        ServiceTicket {

    /**
     * Ticket对应的授权的Service.
     */
    @Getter
    private final Service service;
    @Getter @Setter
    private boolean fromNewLogin;

    protected ServiceTicketImpl(final String id,
                                final TicketGrantingTicket ticket, final Service service,
                                final boolean fromNewLogin, final ExpirationPolicy policy) {
        super(id, ticket, policy);

        if (ticket == null || service == null) {
            throw new IllegalArgumentException(
                    "ticket and service are required parameters");
        }

        this.service = service;
        this.fromNewLogin = fromNewLogin;
    }

    public boolean isExpiredInternal() {
        return this.getGrantingTicket().isExpired();
    }

    public TicketGrantingTicket grantTicketGrantingTicket(final String id,
                                                          final Authentication authentication,
                                                          final ExpirationPolicy expirationPolicy) {
        return new TicketGrantingTicketImpl(id, this.getGrantingTicket(), authentication, expirationPolicy);
    }

}
